package com.android.SummerSchoolProject;

import com.android.SummerSchoolProject.files.FilesChecking;

import android.app.Activity;
import android.content.Intent;
import android.media.MediaPlayer;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.SeekBar;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.SeekBar.OnSeekBarChangeListener;

public class DJConsole extends Activity implements FilesChecking {
	// first page
	private final int SEARCH_FIRST = 1, SEARCH_SECOND = 2;
	private Button btnLaunch, btnSearch1, btnSearch2;
	private TextView tvMusic1, tvMusic2;
	private boolean music1 = false, music2 = false;

	// second page
	private MediaPlayer mp1, mp2;
	private PlayerBoard numberOne, numberTwo;
	private SeekBar volumeA, volumeB, crossfader;
	private int volA, volB, crossf;
	private static String string1,string2;
	private int progress1=0,progress2=0;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
			goFirstPage();
	}

	@Override
    public void onSaveInstanceState(Bundle savedInstanceState) {
    	savedInstanceState.putString("string1", string1);
    	savedInstanceState.putString("string2", string2);
    	savedInstanceState.putInt("volA", volA);
    	savedInstanceState.putInt("volB", volB);
    	savedInstanceState.putInt("crossf", crossf);
    	savedInstanceState.putInt("progress1", progress1);
    	savedInstanceState.putInt("progress2", progress2);
    	
    	super.onSaveInstanceState(savedInstanceState);
    }
    
    
    @Override
    public void onRestoreInstanceState(Bundle savedInstanceState) {
    	super.onRestoreInstanceState(savedInstanceState);
    	string1 = savedInstanceState.getString("string1");
    	string2 = savedInstanceState.getString("string2");
    	volA = savedInstanceState.getInt("volA");
    	volB = savedInstanceState.getInt("volB");
    	crossf = savedInstanceState.getInt("crossf");
    	progress1 = savedInstanceState.getInt("progress1");
    	progress2 = savedInstanceState.getInt("progress2");
    	numberOne.setPBar(progress1);
    	numberTwo.setPBar(progress2);
    	
    }
    
    
	public void goFirstPage() {
		setContentView(R.layout.optionsdjconsole);
		music1 = false;
		music2 = false;
		string1 = "";
		string2 = "";

		btnLaunch = (Button) findViewById(R.id.btnStartConsole);
		tvMusic1 = (TextView) findViewById(R.id.tvMusic1);
		tvMusic2 = (TextView) findViewById(R.id.tvMusic2);
		btnSearch1 = (Button) findViewById(R.id.btnMusic1);
		btnSearch2 = (Button) findViewById(R.id.btnMusic2);
		/******************************************************************/

		//mp1 = MediaPlayer.create(this, R.raw.mystica);
		//mp2 = MediaPlayer.create(this, R.raw.outofcontrol);

		btnLaunch.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				//music1 = music2 = true;
				if (music1 && music2)
					createConsole();
				else {
					Toast chooseFile = Toast.makeText(DJConsole.this,
							"Choose two files", Toast.LENGTH_SHORT);
					chooseFile.show();
				}
			}
		});

		btnSearch1.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				searchIntent(v, SEARCH_FIRST);
			}
		});

		btnSearch2.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				searchIntent(v, SEARCH_SECOND);
			}
		});
	}

	public void createConsole() {
		volA = 50;
		volB = 50;
		crossf = 50;

		setContentView(R.layout.djconsole);
		numberOne = (PlayerBoard) findViewById(R.id.player1);
		numberTwo = (PlayerBoard) findViewById(R.id.player2);

		try {
			/*************/
			 mp1 = MediaPlayer.create(this,
			 Uri.parse(string1));
			 mp2 = MediaPlayer.create(this,
			 Uri.parse(string2));
			mp1.setVolume(0.5f, 0.5f);
			mp2.setVolume(0.5f, 0.5f);
			numberOne.setPlayer(mp1);
			numberTwo.setPlayer(mp2);

			volumeA = (SeekBar) findViewById(R.id.volumeA);
			volumeB = (SeekBar) findViewById(R.id.volumeB);
			crossfader = (SeekBar) findViewById(R.id.crossfader);

			volumeA.setMax(100);
			volumeA.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {
				@Override
				public void onProgressChanged(SeekBar arg0, int arg1,
						boolean arg2) {
					volA = volumeA.getProgress();
					mp1.setVolume((float) volA * (100 - crossf) / 10000,
							(float) volA * (100 - crossf) / 10000);
				}

				@Override
				public void onStartTrackingTouch(SeekBar arg0) {
				}

				@Override
				public void onStopTrackingTouch(SeekBar arg0) {
				}
			});
			volumeB.setMax(100);
			volumeB.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {
				@Override
				public void onProgressChanged(SeekBar arg0, int arg1,
						boolean arg2) {
					volB = volumeB.getProgress();
					mp2.setVolume((float) volB * crossf / 10000, (float) volB
							* crossf / 10000);
				}

				@Override
				public void onStartTrackingTouch(SeekBar arg0) {

				}

				@Override
				public void onStopTrackingTouch(SeekBar arg0) {
				}
			});
			crossfader.setMax(100);
			crossfader
					.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {
						@Override
						public void onProgressChanged(SeekBar arg0, int arg1,
								boolean arg2) {
							crossf = crossfader.getProgress();
							mp1.setVolume(
									(float) volA * (100 - crossf) / 10000,
									(float) volA * (100 - crossf) / 10000);
							mp2.setVolume((float) volB * crossf / 10000,
									(float) volB * crossf / 10000);
						}

						@Override
						public void onStartTrackingTouch(SeekBar arg0) {
						}

						@Override
						public void onStopTrackingTouch(SeekBar arg0) {
						}
					});

			volumeA.setProgress(volA);
			volumeB.setProgress(volB);
			crossfader.setProgress(crossf);
		} catch (Exception e) {
			Toast wrongFile = Toast.makeText(DJConsole.this, "Can't open file" + string1 + string2,
					Toast.LENGTH_LONG);
			wrongFile.show();
			goFirstPage();
		}
	}

	@Override
	protected void onStop() {
		if (numberOne!=null) {
			numberOne.stop();
		}
		if (numberTwo!=null) {
			numberTwo.stop();
		}
		if (mp1 != null) {
			// TODO check how it works with release!
			mp1.release();
		}
		if (mp2 != null) {
			// TODO check how it works with release!
			mp2.release();
		}
		super.onStop();
	}

	@Override
	protected void onPause() {
		if (numberOne != null) {
			numberOne.pause();
		}
		if (numberTwo != null) {
			numberTwo.pause();
		}
		super.onPause();
	}

	@Override
	public boolean checkAudioFormat(CharSequence path) {
		for (String extension : allowedformats) {
			if (extension.equalsIgnoreCase(path.subSequence(path.length() - 3,
					path.length()).toString()))
				return true;
			if (path.subSequence(path.length() - 4, path.length()).toString()
					.equalsIgnoreCase("flac"))
				return true;
		}
		return false;
	}

	@Override
	public void searchIntent(View view, int code) {
		Intent searchMusic = new Intent();
		searchMusic.addCategory(Intent.CATEGORY_OPENABLE);
		searchMusic.setType("text/csv");
		searchMusic.setAction(Intent.ACTION_GET_CONTENT);
		startActivityForResult(
				Intent.createChooser(searchMusic, "select file"), code);
	}

	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		if (resultCode == RESULT_OK) {
			if (checkAudioFormat(data.getData().getPath())) {
				if (requestCode == SEARCH_FIRST) {
					string1 = data.getData().getPath();
					tvMusic1.setText(data.getData().getPath().substring(data.getData().getPath().lastIndexOf('/')+1));
					music1 = true;
				} else if (requestCode == SEARCH_SECOND) {
					tvMusic2.setText(data.getData().getPath().substring(data.getData().getPath().lastIndexOf('/')+1));
					string2 = data.getData().getPath();
					music2 = true;
				}
			} else {
				Toast wrongformat = Toast.makeText(this, "wrong file format",
						Toast.LENGTH_SHORT);
				wrongformat.show();
			}
		}
	}

}
